/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */


package ru.teleology.cubefs.api.reflection;

import ru.teleology.cubefs.api.reflection.list.List;

@NativeType(name = Types.OPERATION_TYPE)
public class Operation extends Typed {
	
	public Operation(long handle) {
		super(handle, false);
	}
	
	public Type owner() {
		long h = owner(getHandle());
		if (peer(h) != null) {
			return (Type) peer(h);
		}
		else {
			return new Type(h);
		}
    }
    
    public Type GetReturnType() {
    	long h = GetReturnType(getHandle());
    	if (h == 0) {
    		throw new RuntimeException("Return type for operration "+owner().name()+"::"+name()+" is not specified");
    	}
    	else if (peer(h) != null) {
			return (Type) peer(h);
		}
		else {
			return new Type(h);
		}
    }
    
    public int GetArgumentsCount() {
    	return GetArgumentsCount(getHandle());
    }
    
    public Parameter GetArgument(int index) {
    	long h = GetArgument(getHandle(), index);
    	if (peer(h) != null) {
			return (Parameter) peer(h);
		}
		else {
			return new Parameter(h);
		}
    }
    
    public Parameter GetArgument(String name) {
    	long h = GetArgument(getHandle(), name);
    	if (peer(h) != null) {
			return (Parameter) peer(h);
		}
		else {
			return new Parameter(h);
		}
    }
   
	public String name() {
		return name(getHandle());
	}
	
    public Typed invoke(Typed target, List arguments) {
    	long h = invoke(getHandle(), target!=null ? target.getHandle() : 0, arguments!=null ? arguments.getHandle() : 0);
    	return h != 0 ? (peer(h) != null ? (Typed)peer(h) : createTyped(h) ) : null;
    }
    
    private Typed createTyped(long h) {
    	return Types.getInstance(h);
    }

    public boolean IsStatic() {
        return IsStatic(getHandle());
    }

    public boolean IsTyped() {
        return IsTyped(getHandle());
    }
    
    private static native long owner(long me);
    
    private static native long GetReturnType(long me);
    
    private static native int GetArgumentsCount(long me);
    private static native long GetArgument(long me, int index);
    private static native long GetArgument(long me, String name);
    
    private static native String name(long me);
    
    private static native long invoke(long me, long target, long arguments);
    
    private static native boolean IsStatic(long me);

    private static native boolean IsTyped(long me);
    
    @Override
    public String toString() {
    	StringBuilder sb = new StringBuilder();
    	sb.append(this.GetReturnType()+" "+this.name()+"(");
		for (int d = 0; d < this.GetArgumentsCount(); d++) {
			Parameter p = this.GetArgument(d);
			sb.append(p.GetParameterType().name()+" "+p.name());
			if (d < this.GetArgumentsCount() - 1) sb.append(", ");
		}
		sb.append(");");
    	
    	return sb.toString();
    }
}
